package com.example.music.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.music.common.R;
import com.example.music.entity.Music;
import com.example.music.service.MusicService;
import com.example.music.service.OssService;
import com.example.music.utils.FileHandle;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lee
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/music/music")
public class MusicController {

    @Autowired
    private MusicService musicService;
    @Autowired
    private OssService ossService;

    @GetMapping("getAll")
    public R getAll() {
        List<Music> musicList = musicService.list();
        return R.ok().data("date", musicList);
    }

    @GetMapping("getTitle")
    public R getTitle(String title) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("title",title);
//       int count= musicService.count(wrapper);
        List<Music> musicList = new ArrayList<>();
        musicList = musicService.getBaseMapper().selectList(wrapper);
        System.out.println(musicList);
        return R.ok().data("date", musicList);
    }

    @GetMapping("getSingger")
    public R getSingger(String singger) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("author",singger);
//       int count= musicService.count(wrapper);
        List<Music> musicList = new ArrayList<>();
        musicList = musicService.getBaseMapper().selectList(wrapper);
        System.out.println(musicList);
        return R.ok().data("date", musicList);
    }

    @GetMapping("pageMusic/{current}/{limit}")
    public R pageListMusic(@PathVariable long current,@PathVariable long limit){
        Page<Music> pageMusic = new Page<>(current, limit);

        musicService.page(pageMusic,null);
        long total=pageMusic.getTotal();
        List<Music> records=pageMusic.getRecords();
        return R.ok().data("total",total).data("records",records);

    }


    /*
        判定是否以及存在
     */
    public boolean isExist(String singer, String title, String fileNmae) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("author",singer);
        wrapper.eq("title",title);
//        wrapper.ge("file_name",fileNmae);
        int count = musicService.count(wrapper);
        System.out.println(count);
        if (count == 0) {
            return true;

        } else {
            return false;
        }
    }


    //上传音乐方法
    @PostMapping("musicupload/{title}/{singger}")
    public R uploadMusic(MultipartFile file, @PathVariable String title, @PathVariable String singger) {
        //获取上传文件 MultipartFile
        String fileName = file.getOriginalFilename();

        if (title == null) {
            title = fileName.substring(fileName.indexOf("-") + 1, fileName.lastIndexOf("."));
            System.out.println("歌曲名 " + title);
        }
        if (singger == null) {
            singger = fileName.substring(0, fileName.indexOf("-"));
            System.out.println("歌手 " + singger);
        }


//        System.out.println("文件名 "+fileName);
//
//        String singer=fileName.substring(0,fileName.indexOf("-"));
//        System.out.println("歌手 "+singer);
//        String title=fileName.substring(fileName.indexOf("-")+1,fileName.lastIndexOf("."));
//
//        System.out.println("歌曲名 "+title);


        if (!isExist(singger, title, fileName)) {
            return R.error().message("音乐已经存在");
        } else {
            //返回上传到oss的路径

            String url = ossService.uploadFileMusic(file);

            Music music = new Music();
            music.setMusicPath(url);
            music.setAuthor(singger);
            music.setFileName(fileName);
            music.setTitle(title);

            boolean flag = musicService.saveOrUpdate(music);
//        musicService.save(music);
            if (flag) {
                return R.ok().message(url);
            } else {
                return R.error();
            }
        }


    }


    //上传音乐方法
    @PostMapping("musicuploadByAuthorFirst")
    @ApiOperation("歌手名在前")
    public R musicuploadByAuthorFirst(MultipartFile file) {
        //获取上传文件 MultipartFile

        String fileName = file.getOriginalFilename();


        System.out.println("文件名 " + fileName);

        String singger = fileName.substring(0, fileName.indexOf("-"));
        System.out.println("歌手 " + singger);
        String title = fileName.substring(fileName.indexOf("-") + 1, fileName.lastIndexOf("."));

        System.out.println("歌曲名 " + title);


        if (!isExist(singger, title, fileName)) {
            return R.error().message("音乐已经存在");
        } else {
            //返回上传到oss的路径

            String url = ossService.uploadFileMusic(file);

            Music music = new Music();
            music.setMusicPath(url);
            music.setAuthor(singger);
            music.setFileName(fileName);
            music.setTitle(title);

            boolean flag = musicService.saveOrUpdate(music);
//        musicService.save(music);
            if (flag) {
                return R.ok().message(url);
            } else {
                return R.error();
            }
        }
    }

    @PostMapping("musicuploadByTitleFirst")
    @ApiOperation( "歌曲名在前")
    public R musicuploadByTitleFirst(MultipartFile file) {
        //获取上传文件 MultipartFile

        String fileName = file.getOriginalFilename();


        System.out.println("文件名 " + fileName);

        String title = fileName.substring(0, fileName.indexOf("-"));

        String singger = fileName.substring(fileName.indexOf("-") + 1, fileName.lastIndexOf("-"));
        System.out.println("歌手 " + singger);
        System.out.println("歌曲名 " + title);


        if (!isExist(singger, title, fileName)) {
            return R.error().message("音乐已经存在");
        } else {
            //返回上传到oss的路径

            String url = ossService.uploadFileMusic(file);

            Music music = new Music();
            music.setMusicPath(url);
            music.setAuthor(singger);
            music.setFileName(fileName);
            music.setTitle(title);

            boolean flag = musicService.saveOrUpdate(music);
            if (flag) {
                return R.ok().message(url);
            } else {
                return R.error();
            }
        }
    }



}

